using System;
using System.Collections;
using SMS.SMPPClient.PDU;
using SMS.SMPPClient.PDU.Outgoing;

namespace SMS.SMPPClient.Util
{
    /// <summary>
    /// Factory to generate destination address byte arrays for the submit_multi.
    /// </summary>
    public sealed class DestAddrFactory
    {
        /// <summary>
        /// Don't instantiate
        /// </summary>
        private DestAddrFactory()
        {
        }

        /// <summary>
        /// Generates an SME destination address.
        /// </summary>
        /// <param name="destAddrTON">Type of Number for destination SME.</param>
        /// <param name="destAddrNPI">Numbering Plan Indicator for destination SME.
        /// </param>
        /// <param name="destAddr">Destination Address for this short message.
        /// Maximum length of 20.</param>
        /// <returns>A DestinationAddresses object from which the byte array
        /// can be retrieved.</returns>
        public static DestinationAddress SMEAddress(
            PDU.PDU.TONType destAddrTON,
            PDU.PDU.NPIType destAddrNPI,
            string destAddr)
        {
            if (destAddr != null && destAddr.Length <= 20)
            {
                ArrayList sme = new ArrayList();
                sme.Add((byte)0x01);
                sme.Add((byte)destAddrTON);
                sme.Add((byte)destAddrNPI);
                sme.AddRange(SMPPRequest.ArrayCopyWithNull(
                                    System.Text.Encoding.ASCII.GetBytes(destAddr)));
                sme.TrimToSize();

                byte[] smeBytes = new Byte[sme.Count];
                sme.CopyTo(smeBytes);
                return new DestinationAddress(smeBytes);
            }
            else
            {
                throw new ArgumentException(
                    "destAddr must be 20 characters or less in SMEAddress.");
            }
        }

        /// <summary>
        /// Generates a distribution list address.  This is also known formally as
        /// dl_name.
        /// </summary>
        /// <param name="distListName">The name of the distribution list.
        /// Maximum length of 20.</param>
        /// <returns>A DestinationAddresses object from which the byte array
        /// can be retrieved.</returns>
        public static DestinationAddress DistributionList(string distListName)
        {
            if (distListName != null && distListName.Length <= 20)
            {
                ArrayList dln = new ArrayList();
                dln.Add((byte)0x02);
                dln.AddRange(SMPPRequest.ArrayCopyWithNull(
                                    System.Text.Encoding.ASCII.GetBytes(distListName)));
                dln.TrimToSize();

                byte[] dlnBytes = new Byte[dln.Count];
                dln.CopyTo(dlnBytes);
                return new DestinationAddress(dlnBytes);
            }
            else
            {
                throw new ArgumentException(
                    "distListName must be 20 characters or less in SMEAddress.");
            }
        }
    }
}
